//
//  LoginVC.m
//  eyr
//
//  Created by Tor Gjøsæter on 6/21/12.
//  Copyright (c) 2012 University of Bergen. All rights reserved.
//

#import "LoginVC.h"



@interface LoginVC ()

@end

@implementation LoginVC
@synthesize loginButton;
@synthesize bgImage;
@synthesize myPopoverController;
@synthesize passwordField;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

	bgImage.alpha = 0.0;
    
    [UIView 
     animateWithDuration:2.5
     delay:0.0
     options: UIViewAnimationOptionCurveEaseIn
     animations:^{
         bgImage.alpha = 0.5;
     }
     completion:nil
     ];
    // Do any additional setup after loading the view.
    
    //Check if pass is set, if not set text disable loginbutton
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    if([defaults objectForKey:@"password"] == nil ){
        NSLog(@"Ingen passord");
        [loginButton setHidden:YES];     
    }
    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"popoverSegueToPasswordVC"])
    {
        // Get reference to the destination view controller
        ChangePasswordVC *vc = [segue destinationViewController];
        
        [vc setDelegate:self];
        
        if ([segue isKindOfClass:[UIStoryboardPopoverSegue class]]) {
            UIStoryboardPopoverSegue *popoverSegue = 
            (UIStoryboardPopoverSegue *)segue;
            self.myPopoverController = popoverSegue.popoverController;
        }
    }
}

-(void)didClickSaveButton {
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if([defaults objectForKey:@"password"] != nil ){
        NSLog(@"Nytt passord, vis loginkey");
        [loginButton setHidden:NO];     
    }
    
    [self.myPopoverController dismissPopoverAnimated:YES];
}

- (IBAction)validatePassword:(id)sender {

}

- (IBAction)checkPassword:(id)sender {
    NSLog(@"Validating");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    if([passwordField.text isEqualToString:[defaults objectForKey:@"password"]]){
        NSLog(@"Correct password");
        [self performSegueWithIdentifier:@"SegueToUserScreen" sender:sender];
    }

}

- (void)viewDidUnload
{
    [self setPasswordField:nil];

    [self setBgImage:nil];
    [self setLoginButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


@end
